<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

require_once(dirname(__FILE__) . '/../../../../PHPImageWorkshop/ImageWorkshop.php');
require_once(dirname(__FILE__) . '/../../../../PHPImageWorkshop/Core/ImageWorkshopLayer.php');
require_once(dirname(__FILE__) . '/../../../../PHPImageWorkshop/Core/ImageWorkshopLib.php');

/**
 * Smarty {image} function plugin
 *
 */
function smarty_function_image($params, &$smarty)
{
	ini_set("memory_limit","512M");
	
	if (!isset($params['src'])) {
		return '<!-- image not set -->';
	} else {
		$baseDir = dirname(__FILE__) . '/../../../../../';
		$filename =  $baseDir . $params['src'];

		if (!file_exists($filename) || is_dir($filename)) {
			$params['src'] = 'fileadmin/templates/img/default.jpg';
			$filename =  $baseDir . $params['src'];
		}
		
		if (file_exists($filename) && !is_dir($filename)) {

			$newImageBase = $baseDir . 'tmp/media/' . $params['width'] . '-' . $params['height'] . '/';
			$folders = explode('/', $params['src']);
			array_pop($folders);
			$folder = $newImageBase . implode('/', $folders);
			if (!file_exists($folder)) {
				mkdir($folder, 0777, TRUE);
			}
			
			$tmpResizeBaseFolder = $baseDir . 'tmp/media/tmp_resized/';
			$tmpResizedFolder = $tmpResizeBaseFolder . implode('/', $folders);
			if (!file_exists($tmpResizedFolder)) {
				mkdir($tmpResizedFolder, 0777, TRUE);
			}
			
			$newPath = $newImageBase . $params['src'];
			if (!file_exists($newPath)) {
				// zistime rozmery obrazka
				$imageSize = getimagesize($filename);
				$actualWidth = $imageSize[0];
				$actualHeight = $imageSize[1];
				
				// vypocitame vysku a sirku, na ktoru treba orezat obrazok
				$widthRatio = $params['width'] / $actualWidth;
				$heightRatio = $params['height'] / $actualHeight;
				if ($widthRatio > $heightRatio) {
					$width = $actualWidth;
					if ($heightRatio) {
						$height = $actualHeight * $heightRatio / $widthRatio;
					} else {
						$height = $actualHeight * $widthRatio;
					}
				} else {
					if ($widthRatio) {
						$width = $actualWidth * $widthRatio / $heightRatio;
					} else {
						$width = $actualWidth * $heightRatio;
					}
					$height = $actualHeight;
				}
				
				// najprv orezeme obrazok aby bol v pomere v akom ho na konci ocakavame
				$cropLayer = \PHPImageWorkshop\ImageWorkshop::initFromPath($filename);
				$cropLayer->cropInPixel($width, $height, 0, 0, 'MM');
				$cropLayer->save($tmpResizeBaseFolder, $params['src'], TRUE, NULL, 100);
				
				// vypocet novej sirky a vysky vo vsetkych pripadoch
				if (isset($params['width']) && isset($params['height'])) {
					$newWidth = $params['width'];
					$newHeight = $params['height'];
				} elseif (isset($params['width'])) {
					$newWidth = $params['width'];
					$newHeight = $height * $params['width'] / $width;
				} elseif (isset($params['height'])) {
					$newHeight = $params['height'];
					$newWidth = $width * $params['height'] / $height;
				} else {
					$newWidth = $width;
					$newHeight = $height;
				}
				
				// resizneme orezany obrazok na pozadovane rozmery
				$resizeLayer = \PHPImageWorkshop\ImageWorkshop::initFromPath($tmpResizeBaseFolder . $params['src']);
				$resizeLayer->resizeInPixel($newWidth, $newHeight);
				$resizeLayer->save($newImageBase, $params['src'], TRUE, NULL, 100);
				
				// odstranime docasny orezany obrazok
				unlink($tmpResizeBaseFolder . $params['src']);
			}

			$img = '<img src="' . str_replace($baseDir, '', $newPath) . '"';
			if ($params['alt']) {
				$img .= ' alt="' . $params['alt'] . '"';
			} else {
				$img .= ' alt=""';
			}

			if ($params['class']) {
				$img .= ' class="' . $params['class'] .'"';
			}

			if ($params['width']) {
				$img .= ' width="' . $params['width'] . '"';
			}
			if ($params['height']) {
				$img .= ' height="' . $params['height'] . '"';
			}

			$img .= ' />';
			return $img;
		} else {
			return '<!-- image ' . $params['src'] . ' not found -->';
		}
	}
}

/* vim: set expandtab: */

?> 